﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection.Emit;
using CityDashboard.Converters;
using Newtonsoft.Json;


namespace CityDashboard.Models
{
    public class ThongKe
    {
        [JsonProperty("HoSo")]
        public ThongKeHoSo HoSo { get; set; }

        [JsonProperty("DuAnCapMoi")]
        public List<DuAnCapMoi> DuAnCapMoi { get; set; }
        [JsonProperty("DoanhNghiepGiaiThe")]
        public List<DoanhNghiepGiaiThe> DoanhNghiepGiaiThe { get; set; }
        [JsonProperty("SoLuongDNTheoLoaiHinhDuAn")]
        public List<SoLuongDNTheoLoaiHinhDuAn> SoLuongDNTheoLoaiHinhDuAn { get; set; }
        [JsonProperty("SoTienTheoLoaiHinhDuAn")]
        public List<SoTienTheoLoaiHinhDuAn> SoTienTheoLoaiHinhDuAn { get; set; }
        /*[JsonProperty("LoaiHinhDN")]
        public List<LoaiHinhDN> LoaiHinhDN { get; set; }
        [JsonProperty("Top5DN")]
        public List<Top5DN> Top5DN { get; set; }
        */

        //Top5NDT
        [JsonProperty("LoaiHinhDN")]
        public List<LoaiHinhDN> LoaiHinhDN { get; set; }
        [JsonProperty("Top5NDT")]
        public List<Top5NDT> Top5NDT { get; set; }
        //

        [JsonProperty("Top10QG")]
        public List<Top10QG> TopQG { get; set; }
        [JsonProperty("DViCapPhep")]
        public List<DViCapPhep> DViCapPhep { get; set; }
        [JsonProperty("HoSoDangGiaiQuyet")]
        public ThongKeHoSoDangGiaiQuyet HoSoDangGiaiQuyet { get; set; }

        public ThongKeLoaiHinhDuAn ThongKeLoaiHinhDuAn
        {
            get
            {
                return new ThongKeLoaiHinhDuAn(SoTienTheoLoaiHinhDuAn, SoLuongDNTheoLoaiHinhDuAn);
            }
        }

        /*public ThongKeTop5NhaDT ThongKeTop5NhaDT
        {
            get
            {
                return new ThongKeTop5NhaDT(Top5DN);
            }
        }
        */
        //Top5NDT
        public ThongKeTop5NhaDT ThongKeTop5NhaDT
        {
            get
            {
                return new ThongKeTop5NhaDT(Top5NDT);
            }
        }
        //

        public List<Top10QG> ThongKe10QG
        {
            get
            {
                var temp = TopQG.OrderByDescending(x => x.Sl).Take(10).ToList();
                int i = 1;
                foreach (var qg in temp)
                {
                    qg.STT = i++;
                }
                return temp.ToList();
            }
        }
    }


    /*public class ThongKeTop5NhaDT : ObservableCollection<TopInvestorModel>
    {
        public List<Top5DN> Top5DN { get; set; }
        public ThongKeTop5NhaDT(List<Top5DN> Top5DN)
        {
            this.Top5DN = new List<Top5DN>();
            int i = 1;
            foreach (var top5DN in Top5DN.OrderByDescending(x => x.TongvonUSD))
            {
                top5DN.STT = i++;
                this.Top5DN.Add(top5DN);
                this.Add(new TopInvestorModel(top5DN.TongvonUSD, top5DN.TenDuAnDautu));
            }

        }
    }
    */
    //Top5NDT
    public class ThongKeTop5NhaDT : ObservableCollection<TopInvestorModel>
    {
        public List<Top5NDT> Top5NDT { get; set; }
        public ThongKeTop5NhaDT(List<Top5NDT> Top5NDT)
        {
            this.Top5NDT = new List<Top5NDT>();
            int i = 1;
            foreach (var top5NDT in Top5NDT.OrderByDescending(x => x.TongvonUSD))
            {
                top5NDT.STT = i++;
                this.Top5NDT.Add(top5NDT);
                this.Add(new TopInvestorModel(top5NDT.TongvonUSD, top5NDT.NhaDauTu));
            }

        }
    }
    //

    public class ThongKeLoaiHinhDuAn
    {


        public int TongSoLuong { get; set; }
        public double TongSoTien { get; set; }

        public ThongKeLoaiHinhDuAn(IEnumerable<SoTienTheoLoaiHinhDuAn> soTien, IEnumerable<SoLuongDNTheoLoaiHinhDuAn> soLuong)
        {
            TongSoLuong = soLuong.Sum(x => x.Sl);
            TongSoTien = soTien.Sum(x => x.Sl);
        }
    }

    public class ThongKeHoSo : List<HoSo>
    {
        public int TongSoHoSoNhan
        {
            get
            {
                return Count != 0 ? this.Sum(x => x.Soluong) : 0;
            }
        }

        public string DaGiaiQuyet
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 1).Sum(x => x.Soluong) : 0;
                var phanTram = (((double)soLuong / TongSoHoSoNhan) * 100).ToString("F2");
                return NumberFormatter.Convert(soLuong);

            }
        }

        public string DangGiaiQuyet
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 0).Sum(x => x.Soluong) : 0;
                var phanTram = (((double)soLuong / TongSoHoSoNhan) * 100).ToString("F2");
                return NumberFormatter.Convert(soLuong);
                //return string.Format("{0} - {1}%", soLuong, phanTram);
            }
        }
        public string DangGiaiQuyetTre
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 0 && x.TTDungHan == 0).Sum(x => x.Soluong) : 0;
                return NumberFormatter.Convert(soLuong);
                //return string.Format("{0} - {1}%", soLuong, phanTram);
            }
        }

        public string DangGiaiQuyetDung
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 0 && x.TTDungHan == 1).Sum(x => x.Soluong) : 0;
                var tong = Count != 0 ? this.Where(x => x.LoaiHs == 0).Sum(x => x.Soluong) : 0;
                //                return NumberFormatter.Convert(soLuong);
                return string.Format("{0} - {1}%", soLuong, NumberFormatter.Convert((((double)soLuong / tong) * 100)));
            }
        }
        public string DangGiaiQuyetTrePhanTram
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 0 && x.TTDungHan == 0).Sum(x => x.Soluong) : 0;
                var tong = Count != 0 ? this.Where(x => x.LoaiHs == 0).Sum(x => x.Soluong) : 0;
                return NumberFormatter.Convert((((double)soLuong / tong) * 100)) + "%";
                //return string.Format("{0} - {1}%", soLuong, phanTram);
            }
        }

        public string DaGiaiQuyetTre
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 1 && x.TTDungHan == 0).Sum(x => x.Soluong) : 0;
                return NumberFormatter.Convert(soLuong);
                //return string.Format("{0} - {1}%", soLuong, phanTram);
            }
        }
        public string DaGiaiQuyetTrePhanTram
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 1 && x.TTDungHan == 0).Sum(x => x.Soluong) : 0;
                var tong = Count != 0 ? this.Where(x => x.LoaiHs == 1).Sum(x => x.Soluong) : 0;
                return NumberFormatter.Convert((((double)soLuong / tong) * 100)) + "%";
                //return string.Format("{0} - {1}%", soLuong, phanTram);
            }
        }

        public string DaGiaiQuyetDung
        {
            get
            {
                var soLuong = Count != 0 ? this.Where(x => x.LoaiHs == 1 && x.TTDungHan == 1).Sum(x => x.Soluong) : 0;
                var tong = Count != 0 ? this.Where(x => x.LoaiHs == 1).Sum(x => x.Soluong) : 0;
                //                return NumberFormatter.Convert(soLuong);
                return string.Format("{0} - {1}%", soLuong, NumberFormatter.Convert((((double)soLuong / tong) * 100)));
            }
        }

        public int HoSoKHDT
        {
            get
            {
                return Count != 0 ? this.Sum(x => x.Soluong) : 0;
            }
        }

        public string TrinhUBNDTong
        {
            get
            {
                var sl = Count != 0 ? this.Where(x => x.TrinhUBND == 1).Sum(x => x.Soluong) : 0;
                //var pt = (((double)sl / HoSoKHDT) * 100).ToString("F2");
                return string.Format("{0}%", NumberFormatter.Convert((((double)sl / HoSoKHDT) * 100)));
            }
        }

        //public int TrinhUBND
        //{
        //    get
        //    {
        //        return Count != 0 ? this.Where(x => x.TrinhUBND == 1).Sum(x => x.Soluong) : 0;
        //    }
        //}
        //public string TrinhUBNDTre
        //{
        //    get
        //    {
        //        var sl = Count != 0 ? this.Where(x => x.TrinhUBND == 1 && x.TTDungHan == 0).Sum(x => x.Soluong) : 0;
        //        //var pt = (((double)sl / HoSoKHDT) * 100).ToString("F2");
        //        return string.Format("({0} trễ)", sl);
        //    }
        //}

        //public string TrinhUBNDDaGiaiQuyet
        //{
        //    get
        //    {
        //        var sl = Count != 0 ? this.Where(x => x.TrinhUBND == 1 && x.LoaiHs == 1).Sum(x => x.Soluong) : 0;
        //        var tong = Count != 0 ? this.Where(x => x.TrinhUBND == 1).Sum(x => x.Soluong) : 0;
        //        var pt = (((double)sl / tong) * 100).ToString("F2");
        //        return string.Format("{0} - {1}%", sl, pt);
        //    }
        //}

        //public string TrinhUBNDDaGiaiQuyetTre
        //{
        //    get
        //    {
        //        var sl = Count != 0
        //            ? this.Where(x => x.TrinhUBND == 1 && x.LoaiHs == 1 && x.TTDungHan == 0).Sum(x => x.Soluong)
        //            : 0;
        //        return string.Format("({0} trễ)", sl);
        //    }
        //}

        //public string TrinhUBNDChuaGiaiQuyet
        //{
        //    get
        //    {
        //        var sl = Count != 0 ? this.Where(x => x.TrinhUBND == 1 && x.LoaiHs == 0).Sum(x => x.Soluong) : 0;
        //        var tong = Count != 0 ? this.Where(x => x.TrinhUBND == 1).Sum(x => x.Soluong) : 0;
        //        var pt = (((double)sl / tong) * 100).ToString("F2");
        //        return string.Format("{0} - {1}%", sl, pt);
        //    }
        //}

        public string KhongUBNDTong
        {
            get
            {
                var sl = Count != 0 ? this.Where(x => x.TrinhUBND == 0).Sum(x => x.Soluong) : 0;

                return string.Format("{0}%", NumberFormatter.Convert((((double)sl / HoSoKHDT) * 100)));
            }
        }

        //public string KhongUBNDTre
        //{
        //    get
        //    {
        //        var sl = Count != 0 ? this.Where(x => x.TrinhUBND == 0 && x.TTDungHan == 0).Sum(x => x.Soluong) : 0;
        //        //var pt = (((double)sl / HoSoKHDT) * 100).ToString("F2");
        //        return string.Format("({0} trễ)", sl);
        //    }
        //}
    }

    public class ThongKeHoSoDangGiaiQuyet : List<HoSoDangGiaiQuyet>
    {

        public List<HoSoDangGiaiQuyet> KetQua
        {
            get { return Count > 0 ? this.Where(x => x.Soluong > 0).ToList() : null; }
        }
    }

    #region Json Models

    public class TinhTrangXL
    {
        [JsonProperty("tentinhtrang")]
        public string TenTinhTrang { get; set; }
        [JsonProperty("ngayxuly")]
        public DateTime NgayXuLy { get; set; }
        [JsonProperty("tenchuyenvien")]
        public string TenChuyenVien { get; set; }
        [JsonProperty("SoBnhan")]
        public string SoBnhan { get; set; }

    }
    public class HoSo
    {
        [JsonProperty("loai_hs")]
        public int LoaiHs { get; set; }
        [JsonProperty("loai_hs_desc")]
        public string LoaiHsDesc { get; set; }
        [JsonProperty("trinhUBND")]
        public int TrinhUBND { get; set; }
        [JsonProperty("trinhUBND_desc")]
        public string TrinhUBNDDesc { get; set; }
        [JsonProperty("TT_DungHan")]
        public int TTDungHan { get; set; }
        [JsonProperty("TT_DungHan_desc")]
        public string TTDungHanDesc { get; set; }
        [JsonProperty("soluong")]
        public int Soluong { get; set; }
    }
    public class DuAnCapMoi
    {
        [JsonProperty("So_DA")]
        public int SoDA { get; set; }
        [JsonProperty("tongvonUSD")]
        public double TongVonUSD { get; set; }
        public string Nam
        {
            get { return DateTime.Now.Year.ToString(); }
        }
    }
    public class DoanhNghiepGiaiThe
    {
        [JsonProperty("So_DA")]
        public int SoDA { get; set; }

        [JsonProperty("tongvonUSD")]
        public double TongVonUSD { get; set; }
        public string Nam
        {
            get { return DateTime.Now.Year.ToString(); }
        }
    }
    public class SoLuongDNTheoLoaiHinhDuAn
    {
        [JsonProperty("TenloaiHinhDT")]
        public string TenLoaiHinhDT { get; set; }
        [JsonProperty("sl")]
        public int Sl { get; set; }
    }
    public class SoTienTheoLoaiHinhDuAn
    {
        [JsonProperty("TenloaiHinhDT")]
        public string TenloaiHinhDT { get; set; }
        [JsonProperty("sl")]
        public double Sl { get; set; }
    }
    public class LoaiHinhDN
    {
        [JsonProperty("TenLoaiDN")]
        public string TenLoaiDN { get; set; }
        [JsonProperty("sl")]
        public int Sl { get; set; }
    }
    public class Top5DN
    {
        public int STT { get; set; }
        [JsonProperty("socndautu")]
        public string Socndautu { get; set; }
        [JsonProperty("TenDuAnDautu")]
        public string TenDuAnDautu { get; set; }
        [JsonProperty("TongvonUSD")]
        public double TongvonUSD { get; set; }
        [JsonProperty("TenQuocTich")]
        public string TenQuocTich { get; set; }
        [JsonProperty("TenDVCP")]
        public string TenDVCP { get; set; }

        [JsonProperty("TenLoaiHinhDT")]
        public string TenLoaiHinhDT { get; set; }
        [JsonProperty("TENDUAN")]
        public string TenDuAn { get; set; }
    }
  
    public class Top5NDT
    {
        public int STT { get; set; }
        [JsonProperty("TentiengViet")]
        public string NhaDauTu { get; set; }
        [JsonProperty("TongvonUSD")]
        public double TongvonUSD { get; set; }
        [JsonProperty("SODKKD_DKHD")]
        public string SODKKD_DKHD { get; set; }
    }


    public class Top10QG
    {
        public int STT { get; set; }

        [JsonProperty("MaQt_Bc")]
        public string MaQG { get; set; }

        [JsonProperty("TenQuocTich_BC")]
        public string TenQuocTichBC { get; set; }

        private double sl;

        [JsonProperty("sl")]
        public double Sl
        {
            get { return sl; }
            set { sl = Math.Round(value, 2); }
        }

        [JsonProperty("VonVN")]
        public double VonVN { get; set; }

        [JsonProperty("VonNN")]
        public double VonNN { get; set; }

        [JsonProperty("soDA")]
        public int SoDA { get; set; }



    }
    public class DViCapPhep
    {
        [JsonProperty("khu")]
        public int Khu { get; set; }
        [JsonProperty("khu_desc")]
        public string KhuDesc { get; set; }
        [JsonProperty("so_DA")]
        public int SoDA { get; set; }
        [JsonProperty("tongvonUSD")]
        public double TongvonUSD { get; set; }
    }
    public class HoSoDangGiaiQuyet
    {
        [JsonProperty("type")]
        public int Type { get; set; }
        [JsonProperty("trangthai_hs")]
        public string TrangthaiHs { get; set; }
        [JsonProperty("type_ttrang")]
        public int TypeTtrang { get; set; }
        [JsonProperty("xuly")]
        public string Xuly { get; set; }
        [JsonProperty("soluong")]
        public int Soluong { get; set; }
    }

    public class HoSoChiTiet
    {
        public int SoTT { get; set; }
        [JsonProperty("sobnhan")]
        public string Sobnhan { get; set; }

        [JsonProperty("NgayNhan")]
        public DateTime NgayNhan { get; set; }
        [JsonProperty("ngayhtra")]
        public DateTime? Ngayhtra { get; set; }
        [JsonProperty("loai_hs")]
        public int LoaiHs { get; set; }
        [JsonProperty("loai_hs_desc")]
        public string LoaiHsDesc { get; set; }
        [JsonProperty("trinhUBND")]
        public int TrinhUBND { get; set; }
        [JsonProperty("trinhUBND_desc")]
        public string TrinhUBNDDesc { get; set; }
        [JsonProperty("dung_han")]
        public int TTDungHan { get; set; }
        [JsonProperty("dung_han_desc")]
        public string TTDungHanDesc { get; set; }
        [JsonProperty("TenDN")]
        public string TenDN { get; set; }
        [JsonProperty("TenDuAn")]
        public string TenDuAn { get; set; }
    }
    public class HoSoDangGiaiQuyetChiTiet
    {
        public int SoTT { get; set; }

        [JsonProperty("type")]
        public int Type { get; set; }
        [JsonProperty("Sobnhan")]
        public string Sobnhan { get; set; }
        [JsonProperty("trangthai_hs")]
        public string TrangthaiHs { get; set; }
        [JsonProperty("type_ttrang")]
        public int TypeTtrang { get; set; }
        [JsonProperty("xuly")]
        public string Xuly { get; set; }
        [JsonProperty("TenDN")]
        public string TenDN { get; set; }
        [JsonProperty("TenDuAn")]
        public string TenDuAn { get; set; }
        [JsonProperty("NgayNhan")]
        public DateTime NgayNhan { get; set; }
        [JsonProperty("ngayhtra")]
        public DateTime? Ngayhtra { get; set; }
    }
    #endregion

}
